<?php

namespace App\Cms\Controller;

class Article extends Base {

    protected $_column_id = 0;

    public function index() {
        
        // 板块ID
        $this->column_id = $column_id = max(intval($this->request->get('id')), $this->_column_id);

        if (!$column_id) {
            fail('参数错误');
        }

        $this->column = $column = Cms()->Column()->getc($column_id);

        if (!$column) {
            fail('参数错误');
        }

        if ($column['type'] == 'url') {
            $this->location($column['url']);
        }
        
        // 获取当前栏目和所有下级栏目的文章
        $column_ids = Cms()->Column()->getTreeNodesIds($column_id);
        $column_ids[] = $column_id;
        
        $filter_keyword = urldecode($this->request->get('filter_keyword'));

        $filter = [];
        if ($filter_keyword) {
            $filter_keyword = Db()->escape($filter_keyword);
            $filter['title'] = ['like' => "%{$filter_keyword}%"];
        }

        $count = Cms()->Article()->countListByColumnIds($column_ids, $filter);

        $this->page_size = 5;
        $pager = $this->_createPager($count, array('id' => $column_id));
        $length = $pager['length'];
        $start = $pager['offset'];
        $this->assign('pager', $pager);

        $this->rows = Cms()->Article()->getListByColumnIds($column_ids, $start, $length, $filter);
        
        if ($path_ids = Cms()->Column()->getParentIds($column_id)) {
            $root_id = $path_ids[0];
            // 父级栏目，用于面包屑导航
            $this->parent_columns = Cms()->Column()->getListInIds($path_ids);
        } else {
            $root_id = $column_id;
        }
        
        // 设置当前顶级导航的栏目ID
        $this->current_nav_id = $root_id;
        
        // 栏目推荐
        $this->column_hots = Cms()->Article()->getHotsByColumnId($column_id, 0, 6);

        $this->column_pvs = Cms()->Article()->getMostPvListByColumnId($column_id, 0, 8);

        // 最近更新
        $this->newests = Cms()->Article()->getList(0, 10);
        
        $this->title = $column['title'];
        $this->keywords = $column['keywords'];
        $this->description = $column['description'];

        $this->display('index_' . $column['type']);
    }

    public function tag() {

        $this->tag_id = $tag_id = trim(urldecode($this->request->get('tag')));

        if (!$tag_id) {
            fail('参数错误');
        }

        $count = Cms()->Article()->Tag()->countArticleIdsByTag($tag_id);

        /* 分页 */
        $pager = $this->_createPager($count, ['tag_id' => $tag_id]);
        $length = $pager['length'];
        $start = $pager['offset'];
        $this->assign('pager', $pager);

        $article_ids = Cms()->Article()->Tag()->getArticleIdsByTag($tag_id, $start, $length);

        if ($article_ids) {

            $filter['id'] = ['in' => $article_ids];

            $this->rows = Cms()->Article()->getList(null, null, $filter);
        }

        $this->tag = $tag = Cms()->Tag()->get($tag_id);
        
        $this->title = "{$tag['title']} - 标签分类";
    }

    public function page() {

        $id = intval($this->request->get('id'));

        $detail = Cms()->Article()->get($id);

        if (!$detail) {
            fail('参数错误');
        }

        if ($detail['type'] == 'url' && $detail['redirect']) {
            $this->location($detail['redirect']);
        }

        $detail['content'] = htmlspecialchars_decode($detail['content']);
        
        Cms()->Article()->fillAssoc($detail);

        $this->detail = $detail;
        
        $tags = Cms()->Article()->Tag()->getListByArticle($detail['id']);
        
        foreach ($tags as &$tag) {
            $tag = Cms()->Tag()->get($tag['tag_id']);
        }
        unset($tag);
        
        $this->tags = $tags;

        $this->column_id = $column_id = $detail['column_id'];

        $this->column = $column = Cms()->Column()->getc($column_id);
        
        $editor = new \WangEditor4();
        $editor->chunk_upload_server = '/cms/upload/chunk';
        $editor->base64_upload_server = '/cms/upload/base64';
        $editor->remote_capture_server = '/cms/upload/capture';
        $editor->simpleModel = true;
        $this->editor = $editor;

        if ($this->request->isPost()) {
            $data = $this->request->get();

            $data['uid'] = intval(\Auth::getAuthedUid('user'));
            $data['article_id'] = $id;
            $data['content'] = $editor->getContent();

            if (!$data['uid'] && !$data['nickname'] = trim($data['nickname'])) {
                fail('请输入您的昵称');
            }

            if (!$data['content'] = trim($data['content'])) {
                fail('请输入回复内容');
            }

            Cms()->Article()->Reply()->add($data);

            succ('已发表');
        }

        Cms()->Article()->incPv($id);
        
        if ($path_ids = Cms()->Column()->getParentIds($column_id)) {
            $root_id = $path_ids[0];
            // 父级栏目，用于面包屑导航
            $this->parent_columns = Cms()->Column()->getListInIds($path_ids);
        } else {
            $path_ids = [];
            $root_id = $column_id;
        }
        
        // 设置当前顶级导航的栏目ID
        $this->current_nav_id = $root_id;

        $this->pre_detail = Cms()->Article()->getPreOne($column_id, $id);

        $this->next_detail = Cms()->Article()->getNextOne($column_id, $id);

        $this->column_hots = Cms()->Article()->getHotsByColumnId($column_id, 0, 6);

        $this->column_pvs = Cms()->Article()->getMostPvListByColumnId($column_id, 0, 8);

        $this->newests = Cms()->Article()->getList(0, 10);

        $replys = Cms()->Article()->Reply()->getListByArticle($id);
        foreach ($replys as &$reply) {
            $reply['uid'] && $reply['nickname'] = QF()->User()->getc($reply['uid'], 'nickname');
            $reply['content'] = htmlspecialchars_decode($reply['content']);
        }
        unset($reply);
        $this->replys = $replys;

        $this->title = "{$detail['title']} - {$column['title']}";
        $this->keywords = $detail['keywords'];
        $this->description = $detail['description'];

        $this->display('page_' . $detail['type']);
    }

}
